using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Mpi.Common.Controls
{
    public partial class DelayedTextBox : TextBox
    {
        private Timer delayTimer = new Timer();

        public int Delay
        {
            get
            {
                return this.delayTimer.Interval;
            }
            set
            {
                this.delayTimer.Interval = value;
            }
        }
        public DelayedTextBox()
        {
            InitializeComponent();
            delayTimer.Tick += new EventHandler(delayTimer_Tick);
            delayTimer.Interval = 500;
        }

        void delayTimer_Tick(object sender, EventArgs e)
        {
            OnDelayedTextChanged();
        }

        //protected override void OnPaint(PaintEventArgs pe)
        //{
        //    // TODO: Add custom paint code here

        //    // Calling the base class OnPaint
        //    base.OnPaint(pe);
        //}

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (delayTimer.Enabled)
                delayTimer.Stop();
            delayTimer.Start();
        }

        public event EventHandler DelayedTextChanged;

        protected virtual void OnDelayedTextChanged()
        {
            delayTimer.Stop();
            if (DelayedTextChanged != null)
                DelayedTextChanged(this, new EventArgs());
        }
    }
}
